package com.security.event;

import lombok.SneakyThrows;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 大忽悠
 * @create 2022/12/22 12:16
 */
public class UserDomainEventPublisher extends DomainEventPublisher {
    private static final String USER_REGISTER_EVENT="u_register";
    private static final String USER_UPDATE_EVENT="u_update";
    private static final String USER_DEL_EVENT="u_del";

    public UserDomainEventPublisher(DataSourceProperties dataSourceProperties) {
        super(dataSourceProperties);
    }

    @SneakyThrows
    public void publishUserRegisterEvent(String username){
         Map<String,String> attr=new HashMap<>();
         attr.put("username",username);
         attr.put("roles","commonUser");
         publishEvent(DomainEvent.builder().eventDesc(USER_REGISTER_EVENT).eventContent(objectMapper.writeValueAsString(attr)).build());
    }

    @SneakyThrows
    public void publishUserUpdateEvent(String newUsername,String oldUsername) {
        Map<String,String> attr=new HashMap<>();
        attr.put("newUsername",newUsername);
        attr.put("oldUsername",oldUsername);
        publishEvent(DomainEvent.builder().eventDesc(USER_UPDATE_EVENT).eventContent(objectMapper.writeValueAsString(attr)).build());
    }

    @SneakyThrows
    public void publishUserDelEvent(String username) {
        Map<String,String> attr=new HashMap<>();
        attr.put("username",username);
        publishEvent(DomainEvent.builder().eventDesc(USER_DEL_EVENT).eventContent(objectMapper.writeValueAsString(attr)).build());
    }
}
